package com.example.emos.api.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.json.JSONUtil;
import com.example.emos.api.common.util.PageUtils;
import com.example.emos.api.common.util.R;
import com.example.emos.api.controller.form.InsertTattooForm;
import com.example.emos.api.controller.form.InsertTattooMoreForm;
import com.example.emos.api.controller.form.SearchTattooByPageForm;
import com.example.emos.api.db.pojo.TbTattoo;
import com.example.emos.api.service.TattooService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/tattoo")
@Tag(name = "TattooController" ,description = "纹身师接口")
public class TattooController {
    @Autowired
    private  TattooService tattooService;
    @GetMapping("searchAllTattoos")
    @Operation(summary = "查询所有纹身师")
//    @SaCheckLogin
    public R searchAllTattoos() {
        ArrayList<HashMap> list = tattooService.searchAllTattoos();
        return R.ok().put("list", list);
    }

        @PostMapping("/searchTattooByPage")
    @Operation(summary = "查询纹身师分页记录")
//    @SaCheckLogin
    public R searchTattooByPage(@Valid @RequestBody SearchTattooByPageForm form){
        if((form.getStartDate()!=null&&form.getEndDate()==null)||(form.getStartDate()==null&&form.getEndDate()!=null)){
            return R.error("startDate和endDate只能同时为空，或者不为空");
        }
        int page= form.getPage();
        int length= form.getLength();
        int start=(page-1)*length;
        HashMap param= JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start",start);
//        param.put("currentUserId", StpUtil.getLoginIdAsInt());
//        if(!(StpUtil.hasPermission("AMECT:SELECT")||StpUtil.hasPermission("ROOT"))){
//            param.put("userId", StpUtil.getLoginIdAsInt());
//        }
        PageUtils pageUtils=tattooService.searchTattooByPage(param);
        return R.ok().put("page",pageUtils);
    }
    @PostMapping("/insertTattoos")
    @Operation(summary = "批量添加纹身师")
    public R insertTattoos(@Valid @RequestBody InsertTattooMoreForm ff){
        InsertTattooForm[] forms=ff.getInsertTattooForms();
        ArrayList<TbTattoo> list=new ArrayList<>();
        for(InsertTattooForm form:forms){
           TbTattoo tbTattoo= JSONUtil.parse(form).toBean(TbTattoo.class);
//           tbTattoo.setStatus();
            if(tbTattoo.getSex()==null){
                tbTattoo.setSex("男");
            }

            list.add(tbTattoo);
        }
       int rows= tattooService.insertTattoos(list);
        return R.ok().put("rows",rows);
    }
    @PostMapping("/updateTattooByGlobalId")
    @Operation(summary = "更新纹身师")
    public R updateTattooByGlobalId(@Valid @RequestBody InsertTattooForm form){
        HashMap param= JSONUtil.parse(form).toBean(HashMap.class);
       Object sex=  param.get("sex");
       if(sex==null){
           param.replace("sex","男");
       }
      int row= tattooService.updateTattooByGlobalId(param);
       return R.ok().put("rows",row);
    }
}
